package com.bnlr.lrparking.appui.stall;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.appui.search.SeachActiity;
import com.bnlr.lrparking.constant.LrConstant;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.adapter.SelectDevelopersAdapter;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.constant.IntentConstant;
import com.bnlr.lrparking.model.SelectDevelopersM;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.widget.refresh.RefreshHolderUtil;
import com.bnlr.lrparking.widget.refresh.XRefreshLayout;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hp on 2017/10/13.
 * 选择楼盘
 */

public class SelectDevelopersActivity extends BaseActivity {

    TextView tvTitle;
    private RecyclerView rv_select_developers;
    private RelativeLayout rely_nodata;
    private SelectDevelopersAdapter selectDevelopersAdapter;
    private Intent intent;
    private String dealType;
    private String cityName = "";
    private RelativeLayout developers_select_adress;
    private RelativeLayout plot_search;
    private String keyword="";
    private EditText et_title_search;
    private XRefreshLayout swipeRefreshLayout;
    private int currentPage = 1;
    private List<SelectDevelopersM.DataBean> dataList = new ArrayList<>();
    private String strSeach="";
    private static final int REQUEST_SEARCH_CODE = 1600;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selectdevelopers);
        initView();
    }

    private void initView() {
        cityName = AppConfig.getInstance().getString("strCity", "");
        getDeveloperList(keyword,currentPage);
        Intent intent1 = getIntent();
        dealType = intent1.getStringExtra("dealType");
        tvTitle = (TextView) findViewById(R.id.tv_nav_title);
        tvTitle.setText("选择楼盘");
        et_title_search=(EditText)findViewById(R.id.et_title_search);
        et_title_search.setText("搜索小区、楼盘或地址");
//        developers_adress = (TextView) findViewById(R.id.developers_adress);
        rv_select_developers = (RecyclerView) findViewById(R.id.rv_select_developers);
        rely_nodata = (RelativeLayout) findViewById(R.id.rely_nodata);
        developers_select_adress = (RelativeLayout) findViewById(R.id.developers_select_adress);
        plot_search=(RelativeLayout)findViewById(R.id.plot_search);
        swipeRefreshLayout = (XRefreshLayout) findViewById(R.id.swipe_develop);
        swipeRefreshLayout.setRefreshViewHolder(RefreshHolderUtil.getRefreshHolder(this));
        swipeRefreshLayout.setDelegate(new XRefreshLayout.BGARefreshLayoutDelegate() {
            @Override
            public void onBGARefreshLayoutBeginRefreshing(XRefreshLayout refreshLayout) {
                currentPage = 1;
                getDeveloperList(keyword,currentPage);
            }

            @Override
            public boolean onBGARefreshLayoutBeginLoadingMore(XRefreshLayout refreshLayout) {
                return false;
            }

        });
        swipeRefreshLayout.beginRefreshing();
        rv_select_developers.setLayoutManager(new LinearLayoutManager(this));
        selectDevelopersAdapter = new SelectDevelopersAdapter(dataList);
        rv_select_developers.setAdapter(selectDevelopersAdapter);
        selectDevelopersAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getDeveloperList(keyword,currentPage);
            }
        },rv_select_developers);
        et_title_search.setOnClickListener(this);
        developers_select_adress.setOnClickListener(this);
//        developers_adress.setText(cityName);
        selectDevelopersAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                intent = new Intent(SelectDevelopersActivity.this, StallDealActivity.class);
                intent.putExtra("dealType", dealType);
                intent.putExtra("spaceowertype", "1");
                intent.putExtra("plotName", dataList.get(position).getName());
                intent.putExtra("developId", dataList.get(position).getDevelopId() + "");
                intent.putExtra("parkingId", dataList.get(position).getId() + "");
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.developers_select_adress:
                Intent intent = new Intent(this,SeachActiity.class);
                intent.putExtra(IntentConstant.INTENT_STALL_DEAL_SEARCH,keyword);
                startActivityForResult(intent,REQUEST_SEARCH_CODE);

                break;
            case R.id.et_title_search:
//                Intent intent=new Intent(this,SeachActiity.class);
//                intent.putExtra("dealType",dealType);
//                intent.putExtra("developers","1");
//                intent.putExtra("global","0");
//                startActivityForResult(intent,1);
                break;
            default:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            if (requestCode == REQUEST_SEARCH_CODE && resultCode == IntentConstant.SEARCH_RESULT_CODE) {
                keyword = data.getStringExtra(IntentConstant.INTENT_STALL_DEAL_SEARCH);
                if (TextUtils.isEmpty(keyword)) {
                    keyword = "";
                }
                currentPage = 1;
                getDeveloperList(keyword,currentPage);
            }
        }

    }

    //获取楼盘列表
    private void getDeveloperList(String keyword, final int page) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetInSaleParking");
            mRequest.add("parkId", "0");
            mRequest.add("cityName", cityName);
            mRequest.add("keyword", keyword);
            mRequest.add("pageindex", page);
            mRequest.add("developerId", "0");
            Log.e("搜索",keyword);
            Log.i("TAG", "getDeveloperList "+ mRequest);
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<SelectDevelopersM>(baseContext, true, SelectDevelopersM.class) {
                        @Override
                        public void doWork(final SelectDevelopersM data, String code) {
                            if (code.equals("1")) {
                                if (selectDevelopersAdapter != null) {
                                    if (data != null && data.getData().size() != 0) {
                                        rely_nodata.setVisibility(View.GONE);
                                        if (page == 1) {
                                            dataList = data.getData();
                                            selectDevelopersAdapter.setNewData(data.getData());
                                        } else {
                                            if (strSeach.equals("")) {
                                                dataList.addAll(data.getData());
                                                selectDevelopersAdapter.notifyDataSetChanged();
                                            }
                                        }
                                        selectDevelopersAdapter.loadMoreComplete();
                                    } else {
                                        if (page == 1) {
                                            dataList.clear();
                                            selectDevelopersAdapter.notifyDataSetChanged();
                                            rely_nodata.setVisibility(View.VISIBLE);
                                        } else {
                                            selectDevelopersAdapter.loadMoreEnd();
                                        }
                                    }
                                }

                            } else {
                                ToastUtils.showShort(data.getMsg()+"");
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (swipeRefreshLayout != null) {
                                    swipeRefreshLayout.endRefreshing();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {
        }
    }
}
